/*
 * Proto Streamer
 * Copyright (C) 2009 John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package p.s;

import alto.io.u.Array;
import alto.io.u.Lobjmap;
import java.util.Enumeration;

/**
 * Server scope application class loaders' cache. 
 * 
 * @author jdp
 */
public final class Apps
    extends Thread
    implements Millis
{

    private final static Apps Instance = new Apps();
    static {
        Instance.start();
    }
    public final static ClassLoader Get(Request request){
        ClassLoader cl = Instance.get(request);
        request.setContextClassLoader(cl);
        return cl;
    }

    private volatile long cycle = (5 * Minutes);

    private final Lobjmap classloaders;

    private Apps(){
        super(p.s.Group.Top,"Apps");
        this.setDaemon(true);
        this.setPriority(MIN_PRIORITY);
        this.classloaders = new Lobjmap();
    }


    public ClassLoader get(Request request){
        String identity = request.getHostName();
        if (null == identity)
            identity = "null";

        Lobjmap classloaders = this.classloaders;
        classloaders.lockReadEnter();
        try {
            ClassLoader classLoader = (ClassLoader)classloaders.get(identity);
            if (null != classLoader)
                return classLoader.mark();
        }
        finally {
            classloaders.lockReadExit();
        }

        classloaders.lockWriteEnter();
        try {
            ClassLoader classLoader = (ClassLoader)classloaders.get(identity);
            if (null != classLoader)
                return classLoader.mark();
            else {
                classLoader = new ClassLoader(request);
                classloaders.put(identity,classLoader);
                return classLoader;
            }
        }
        finally {
            classloaders.lockWriteExit();
        }
    }

    public void run(){
        Lobjmap classloaders = this.classloaders;
        try {
            Array list = new Array();
            while (true){
                Thread.sleep(this.cycle);
                long time = System.currentTimeMillis();
                classloaders.lockReadEnter();
                try {
                    Enumeration elements = classloaders.elements();
                    while (elements.hasMoreElements()){
                        ClassLoader classloader = (ClassLoader)elements.nextElement();
                        if (classloader.hasExpired(time))
                            list.add(classloader);
                    }
                }
                finally {
                    classloaders.lockReadExit();
                }
                if (list.isNotEmpty()){
                    classloaders.lockWriteEnter();
                    try {
                        for (int cc = 0, count = list.size(); cc < count; cc++){
                            ClassLoader classloader = (ClassLoader)list.get(cc);
                            if (classloader.hasExpired(time)){
                                classloaders.remove(classloader.id);
                            }
                        }
                    }
                    finally {
                        classloaders.lockWriteExit();
                    }
                    list.clear();
                }
            }
        }
        catch (InterruptedException interrupt){
            return;
        }
    }
}
